<template>
  <div>
    <el-aside width="240px" class="aside_rolelist">
      <div class="asidelist">
        <div class="hd">
          <div>门禁点</div>
          <div>
            <el-button
              class="header-icon el-icon-plus"
              type="primary"
              plain
              @click="dialogVisible=true"
            >添加</el-button>
          </div>
        </div>
        <div class="role-container" id="rolelist">
          <el-tree
            :data="dataList"
            show-checkbox
            node-key="id"
            :highlight-current="true"
            @node-click="nodebg"
            :expand-on-click-node="false"
            :props="defaultProps"
          >
            <span class="custom-tree-node" slot-scope="{ node, data }">
              <span class="treefont" @click="getById(data)">{{ node.label }}</span>
              <span class="bticon">
                <el-dropdown>
                  <i class="el-icon-s-operation"></i>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item>
                      <span style="color:#409EFF" class="el-icon-edit" @click="() => edit(data)">编辑</span>
                    </el-dropdown-item>
                    <el-dropdown-item>
                      <span
                        style="color:#F56C6C"
                        class="el-icon-delete"
                        @click="() => remove(node, data)"
                      >删除</span>
                    </el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </span>
            </span>
          </el-tree>
        </div>
      </div>
      <div class="dialog-box">
        <el-dialog title="门禁点" :visible.sync="dialogVisible" width="35%">
          <el-form ref="formData" :model="formData" :rules="rules" label-width="90px">
            <el-form-item label="名称" prop="name">
              <el-input v-model="formData.name"></el-input>
            </el-form-item>
          </el-form>
          <span slot="footer" class="dialog-footer">
            <el-button @click="closeVisible('formData')">取 消</el-button>
            <el-button type="primary" @click="addsaveClick('formData')">确 定</el-button>
          </span>
        </el-dialog>
      </div>
    </el-aside>
  </div>
</template>
<script>
import bus from "../../../common/bus";
export default {
  data() {
    return {
      //角色列表数据
      rules: {
        name: [{ required: true, message: "名称不能为空", trigger: "blur" }]
      },
      dataList: [],
      defaultProps: {
        label: "name"
      },
      dialogVisible: false,
      formData: {
        name: ""
      },
      formeditData: {},
      projectDoor_Id: -1,
      projectId:localStorage.getItem("project_Id")
    };
  },
  created(){ 
    this.getData();
  },
  methods: {
    nodebg(val) {
      bus.$emit("putrole", val.project_Role_Id);
    },
    getData() {
      this.$axios.get('/ProjectDoor/Project/'+this.projectId).then(res =>{
        if (res.data.hasError == false) {
          this.dataList = [...res.data.data]
        }
      })
    },
    addsaveClick(formData) {
      this.formData = {
        name: this.formData.name,
        projectId: this.projectId
      }
      this.$refs[formData].validate(valid => {
        if (valid) {
          if (this.projectDoor_Id == -1) {
            this.$axios.post('/ProjectDoor/',this.formData)
              .then(res =>{
                if (res.data.hasError == false) {
                  this.$message.success("添加成功！");
                  this.getData();
                  this.$refs[formData].resetFields();
                }
              })
          }else {
            this.$axios.put(`/ProjectDoor/${this.projectDoor_Id}`,this.formData)
              .then(res =>{
                if (res.data.hasError == false) {
                  this.$message.success("修改成功！");
                  this.$refs[formData].resetFields();
                  this.projectDoor_Id = -1;
                  this.getData();
                }
              })
          }
          this.dialogVisible = false;
        }
      });
    },
    edit(data) {
      this.dialogVisible = true;
      this.projectDoor_Id = data.projectDoorId;
      var list = {};
      if (this.projectDoor_Id != -1) {
        this.$axios.get(`/ProjectDoor/${this.projectDoor_Id}`)
        .then(res =>{
          if (res.data.hasError == false) {
            list = res.data.data;
            this.formData = {
              name: list.name
            }
          }
        })
      }
    },
    remove(node, data) {
      this.$confirm("是否确认删除？", {confirmButtonText: "确定",cancelButtonText: "取消"})
        .then(() => {
          this.$axios.delete(`/ProjectDoor/${data.projectDoorId}`)
            .then(res => {
              if (res.status == 200) {
                this.$message({type: "success",message: "删除成功!"});
                this.getData();
              }
            });
        })
        .catch(() => {
          this.$message({ type: "info",message: "已取消删除"});
        });
    },
    closeVisible(formData){
      this.$refs[formData].resetFields();
      this.dialogVisible = false
    },
    getById(data){
      bus.$emit('getTicketTypeId',data.ticketTypeId)//传给兄弟组件
    }
  }
};
</script>
<style lang="scss" scope>
.asidelist {
  background-color: #fff;
  border-radius: 5px;
  width: 100%;
  height: 100%;
  .hd {
    height: 60px;
    border-bottom: 1px solid #ebeef5;
    box-sizing: border-box;
    padding: 0 10px;
    line-height: 60px;
    display: flex;
    justify-content: space-between;
  }
  .el-tree-node__expand-icon.is-leaf {
    color: f2f2f2;
  }
  .custom-tree-node {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: space-between;
    font-size: 14px;
    padding-right: 18px;
  }
  .el-checkbox__inner {
    width: 0;
  }
  .el-tree-node__content {
    height: 40px;
  }
}
.aside_rolelist {
  height: 100%;
}
.el-dialog {
  .el-dialog__body{
    padding: 20px;
  }
  height: calc(100% - 200px);
  box-sizing: border-box;
  overflow: auto;
}
</style>